﻿//element cache
var J_searchcard = $("#J_searchcard");
var J_searchcard_popup = $("#J_searchcard_popup");
var J_searchcard_popup_noresult = $("#J_searchcard_popup_noresult");
var J_favorite = $("#J_favorite");
var J_form_buy = $("#J_form_buy");
var J_form_quantity = $("#J_form_quantity");

$(document).ready(function () {
    //保障
    tab($("#J_protect"));
    //产品属性
    tab($("#J_game_attr"));

    //数量
    J_form_quantity.focus(function () {
        var t = $(this);
        t.select();
    }).blur(function () {
        //格式
        var t = $(this);
        var val = t.val().replace(/(^\s*)|(\s*$)/g, "");
        var reg = /[^\d]$/;
        if (val.length > 0) {
            val = val.replace(reg, "");
            if (val == "0") { val = ""; };
            t.val(val);
        };
        //判定数量
        var all = parseInt(t.attr("data-max"));
        if (isNaN(all)) { all = 0 };
        var per = parseInt(val);
        if (isNaN(per)) { per = 0 };
        //报错
        if ((all > 0 && per > all) || per < 1) {
            t.nextAll(".form-message").addClass("form-message-showerror");
            return;
        };
        t.nextAll(".form-message").removeClass("form-message-showerror");
        return;
    }).keyup(function (e) {
        //格式
        var t = $(this);
        var val = t.val().replace(/(^\s*)|(\s*$)/g, "");
        var reg = /[^\d]$/;
        if (val.length > 0) {
            val = val.replace(reg, "");
            if (val == "0") { val = ""; };
            t.val(val);
        };
        //判定数量
        var all = parseInt(t.attr("data-max"));
        if (isNaN(all)) { all = 0 };
        var per = parseInt(val);
        if (isNaN(per)) { per = 0 };
        //向下
        if (e.keyCode == 40) {
            per += -1;
            if (per > 0) { t.val(per); };
            if (per < 1) { per += 1; };
        };
        //向上
        if (e.keyCode == 38) {
            per += 1;
            if (all > 0 && per <= all) { t.val(per); };
            if (all > 0 && per > all) { per += -1; };
        };
        //报错
        if ((all > 0 && per > all) || per < 1) {
            t.nextAll(".form-message").addClass("form-message-showerror");
            return;
        };
        t.nextAll(".form-message").removeClass("form-message-showerror");
        return;
    });

    //表单
    J_form_buy.submit(function () {
        var t = $(this);
        //验证填写信息
        var fields = t.find("input:visible");
        var count = fields.length;
        for (var i = 0; i < count; i++) {
            field = fields.eq(i);
            if (field.nextAll(".form-message-showerror").length < 1) {
                field.focus();
                field.blur();
            };
        };
        if (t.find(".form-message-showerror").length > 0) {
            return false;
        };
    });

    //收藏
    J_favorite.attr("data-href", J_favorite.attr("href"));
    J_favorite.attr("href", "javascript:void(0)");
    J_favorite.click(function () {
        var r = "r=" + Math.random();
        var t = $(this);
        var url = t.attr("data-href");
        url += (url.indexOf("?") > 0) ? "&" + r : "?" + r;
        var width = t.attr("data-width") + "px";
        var height = t.attr("data-height") + "px";
        $("#J_popup").show().css({ left: t.offset().left - 4, top: t.offset().top - 4, width: width, height: height }).find(".popup-iframe").attr({ "src": url, "width": width, "height": height });
        $("#J_popup").find(".popup-close").click(function () { $(this).parent().hide(); });
    });

    //购买历史纪录
    var J_gethistory = $("#J_gethistory");
    var J_showhistory = $("#J_showhistory");
    J_gethistory.hover(function () {
        if (J_showhistory.attr("src").length < 1) {
            J_showhistory.attr("src", J_showhistory.attr("data"));
            J_showhistory.load(function () {
                $(this).height($(this).contents().find("body").height());
            });
        };
    });

    //返回顶部
    var J_top = $("#J_top");
    var tops = J_top.offset();
    $(window).scroll(function () {
        J_top.animate({ top: $(window).scrollTop() + tops.top }, { queue: false, duration: 500 })
    });
    J_top.click(function () { $("html,body").animate({ scrollTop: '0' }, 800) });

});

//tab
function tab(element) {
    element.find(".tab-holder").children().mouseover(function () {
        $(this).parent().find(".tab-current").removeClass("tab-current");
        $(this).addClass("tab-current");
        var eq = $.inArray(this, $.makeArray(element.find(".tab-holder").children()));
        element.find(".tab-panel").children().hide().eq(eq).show();
    }).eq(0).mouseover();
    var href = element.find(".tab-holder a");
    if (href.attr("href") == "" || href.attr("href") == "#" || href.attr("href") == null) { href.attr("href", "javascript:void(0)"); }
};
//分页
function gotoPage(page) {
    var form = document.getElementById("searchForm");
    var currentPage = document.getElementById("currentPage");
    currentPage.value = page;
    form.submit();
}

function changePageSize(pagesize) {
    var form = document.getElementById("searchForm");
    var pagesizeInput = document.getElementById("pagesize");
    var currentPage = document.getElementById("currentPage");
    currentPage.value = 1;
    pagesizeInput.value = pagesize;
    form.submit();
}

function gotoPageByNum() {
    var form = document.getElementById("searchForm");
    var currentPage = document.getElementById("currentPage");
    var gopagenum = document.getElementById("gopagenum");
    currentPage.value = gopagenum.value;
    form.submit();
}

function inputSuitNum(totalPage) {
    var gopagenum = document.getElementById("gopagenum");
    gopagenum.value = gopagenum.value.replace(/[^\d]/g, '');
    if (gopagenum.value > totalPage || gopagenum.value < 1) {
        gopagenum.value = "";
    }
}